package com.stone.business.lsap.action;

/**
 * -----临时安排  ACTION---
 */
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.lsap.pojo.TLsap;
import com.stone.business.lsap.pojo.TLsapBz;
import com.stone.business.lsap.service.LsapService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class LsapAction extends BaseAction {

	private LsapService lsapService;

	private TLsap entity;

	private String id;
	private String status;

	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	private List<TDictionary> sblxList;
	private List<TLsapBz> lsapbzList;

	private String hiddenids;

	/**
	 * 进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initLsapDetail() throws AppException {

		// 修改进入
		if (!"0".equals(id.toString())) {

			this.entity = (TLsap) lsapService.findById(Long.parseLong(id),
					TLsap.class.getName());

			// 新建进入
		} else {

			this.entity = new TLsap();
			this.entity.setStatus("0");
			this.entity.setState(super.getSearchBean().getCon19());
			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
		}

		setDictionary();
		
		if ("0".equals(this.entity.getState())) {
			return "SC_SUCCESS";
		} else {
			return "GL_SUCCESS";
		}

	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveLsap() throws AppException {

		Date confDate = DateFormatTool.formatToDate(lsapService
				.getSysdate("yyyy-MM-dd HH24:mi"), true);

		if (this.entity.getId() == 0) {
			this.entity.setOuid(super.getSession().getAttribute(
					IConstant.SESSION_TORGAN_CODE).toString());
		}

		String rtn = lsapService.doSave(this.entity, this.lsapbzList,
				this.hiddenids);

		setDictionary();

		super.setInfo(rtn);

		if ("0".equals(this.entity.getState())) {
			return "SC_SUCCESS";
		} else {
			return "GL_SUCCESS";
		}

	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteLsap() throws AppException {

		lsapService.doDelAll(id, TLsap.class.getName());
		lsapService.deleteByDHql("lsapid in (" + id + ")", null, TLsapBz.class
				.getName());

		return "DELETE_SUCCESS";
	}

	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmLsap() throws AppException {
		lsapService.updateByDHql("status = '"
				+ super.getRequest().getParameter("flag") + "'", "id in ("
				+ this.id + ")", null, null, TLsap.class.getName());
		return "DELETE_SUCCESS";
	}

	/**
	 * 获得字典项目
	 */
	private void setDictionary() {

		// 变电站
		this.bdzList = lsapService.findListByHql(" from TBdz order by sortid");
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/
		if(this.bdzList != null&&this.bdzList.size() > 0){
			for(int i=0;i<this.bdzList.size();i++){
				TBdz tbdz = this.bdzList.get(i);
				if(tbdz != null){
					tbdz.setBdzname(tbdz.getDydj() + "Kv" + tbdz.getBdzname());
				}
			}
		}
		/*-----------add by fanghf 2012.3.15变电站下拉列表显示由电压等级和变电站名称拼接起来begin-----------*/		
		// 电压等级
		this.dydjList = lsapService
				.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = lsapService
				.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");

		TOrgan o = (TOrgan) super.getSession().getAttribute(
				IConstant.SESSION_TORGAN);
		String where = (o.getOrganCode().toString().length() <= 6 || "1"
				.equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? ""
				: " and organCode = '" + o.getOrganCode().toString() + "'";

		// 班组
		List<TOrgan> list = lsapService
				.findListByHql(" from TOrgan where organNameEn like '%B' "
						+ where + " order by id");

		if ("0".equals(id)) {

			this.lsapbzList = new ArrayList<TLsapBz>();
			int i = 0;
			for (TOrgan organ : list) {
				TLsapBz bz = new TLsapBz();
				bz.setId(Long.parseLong("0"));
				bz.setDelflag("0");
				bz.setBzid(organ.getId());
				bz.setBzname(organ.getOrganName());
				this.lsapbzList.add(bz);
				i++;
			}
		} else {
			where = (o.getOrganCode().toString().length() <= 6 || "1"
					.equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? ""
					: " and bzid  = '" + o.getId().toString() + "'";
			this.lsapbzList = lsapService
					.findListByHql(" from TLsapBz where lsapid = '"
							+ entity.getId() + "'" + where
							+ " order by delflag");
		}

	}
/**
	 * 班组
	 * @return
	 * @throws AppException
	 */	
	public void getBzRw()  throws IOException, AppException {
		
		//TPlanBz planBz = (TPlanBz) planService.findById(Long.parseLong(id), TPlanBz.class.getName());
		// 计划班组
		this.lsapbzList = lsapService.findListByHql(" from TLsapBz where delflag='0' and lsapid = '" + id + "' order by delflag");
		
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(lsapbzList, jsonConfig);

        super.getResponse().getWriter().print(json.toString());
	}
	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public void setLsapService(LsapService lsapService) {
		this.lsapService = lsapService;
	}

	public TLsap getEntity() {
		return entity;
	}

	public void setEntity(TLsap entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<TLsapBz> getLsapbzList() {
		return lsapbzList;
	}

	public void setLsapbzList(List<TLsapBz> lsapbzList) {
		this.lsapbzList = lsapbzList;
	}

	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}

}
